User name and computer name


You can get current user name by using GetUserName, and you can get computer name
by using
GetComputerName.

Example:

- Drop a button and two lables.
- At button's OnClick event write:

var
PUserName: PChar;
PComputerName: PChar;
Size: LongWord;
begin
Size:= 100;
PUserName:= StrAlloc(100);
PComputerName:= StrAlloc(100);

if
GetUserName(PUserName, Size) then
  Label1.Caption:= PUserName
else
  Label1.Caption:= 'There is no logged user';

Size:= 100;
if
GetComputerName(PComputerName, Size) then
  Label2.Caption:= PComputerName
else
  Label2.Caption:= 'Error while getting computer name';

StrDispose(PUserName);
StrDispose(PComputerName);


Notes:

- GetUserName function returns true if operating is successed, and returns false
if there is no logged user for example the user presses cancel on Windows login window.

See also:

Windows temporary directory
Windows directory